package com.schoolvisitors.response;

import com.schoolvisitors.context.UserContext;
import com.schoolvisitors.entity.User;
import com.schoolvisitors.exception.ServiceException;
import com.schoolvisitors.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class ResponseBodyAdvice implements org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object> {
    private final UserService userService;

    public ResponseBodyAdvice(UserService userService) {
        this.userService = userService;
    }

    /**
     * 控制哪些类需要进行拦截
     * @param returnType returnType
     * @param converterType 转换器类型
     * @return 返回true进行拦截 false不进行拦截
     */
    @Override
    public boolean supports(@NonNull MethodParameter returnType, @NonNull Class converterType) {
        //开关处于开启状态  是get请求
        //使用MethodParameter参数判断注解信息
        //System.out.println("returnType: " + returnType);
        //System.out.println("converterType: " + converterType);
        //符合此Get请求才进行织入通知

        return true;
    }

    /**
     * 在控制层返回到客户端之前对数据处理
     * @param body 要返回的原始数据
     * @param returnType ?
     * @param selectedContentType 内容类型
     * @param selectedConverterType 转换器类型
     * @param request 请求
     * @param response 响应
     * @return 最终返回的数据
     */
    @Override
    public Object beforeBodyWrite(Object body, @NonNull MethodParameter returnType, @NonNull MediaType selectedContentType, @NonNull Class selectedConverterType, @NonNull ServerHttpRequest request, @NonNull ServerHttpResponse response) {
        if (body != null) {
            String logBody = body.toString();
            if (logBody.length() > 256){
                logBody = logBody.substring(0, 256);
            }
            log.info("{} response: {}", request.getURI(), logBody);
        }
//        if (body instanceof ResponseEntity){
//            return body;
//        }
//        if (body instanceof ResultBody) {
//            return body;
////        } else if(body instanceof LinkedHashMap){
////            return ResultBody.fail(body.toString());
//        } else {
//            log.info("需要包装 {}", body.getClass());
//            //ResultBody.success(ResultBody.success().put("data", body));
//            return ResultBody.success().put("data", body);
//        }

        return body;
    }

    /**
     * 对ServiceException类型的错误进行处理
     * @param request 请求
     * @param exception 异常对象
     * @return 要返回到客户端的数据
     */
    @ResponseBody
    @ExceptionHandler(value = ServiceException.class)
    public ResultBody handlerGlobeException(HttpServletRequest request, ServiceException exception) {
        return ResultBody.fail(exception.getResultCode());
    }
}
